//
//  SchoolSearchResultCtrl.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/5/6.
//  Copyright © 2016年 game. All rights reserved.

#import "SchoolSearchResultCtrl.h"
#import "NewsCell.h"
#import "ShoolDataModel.h"
#import "AFHTTPSessionManager.h"
#import "PageInfoModel.h"
#import "VideoDescrib.h"
#import "NewestAticleCtr.h"
#import "RootTableview.h"
#import "MJRefresh.h"
@interface SchoolSearchResultCtrl ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *TextField;
@property (nonatomic,strong) RootTableview *tableview;
@property (nonatomic,assign) NSInteger count;
@property (nonatomic,strong) NSMutableArray *listAry;
@property (nonatomic,strong) ShoolDataModel  *model;
@property (nonatomic,strong) NSDictionary *seachDic;
@property (nonatomic,strong) PageInfoModel *infoModel;
@property (nonatomic,assign) CGFloat cellHight;
@end

@implementation SchoolSearchResultCtrl

-(NSMutableArray *)listAry{
    if (!_listAry) {
        _listAry = [NSMutableArray array];
    }
    return _listAry;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.app.myTabBar.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
    StatusBlack;
}
- (IBAction)backClIck:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.frame = [UIScreen mainScreen].bounds;
    self.TextField.delegate=self;
    self.TextField.leftView = [self textFileLweftView];
    
    self.tableview = [[RootTableview alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
    [self.tableview registerClass:[NewsCell class] forCellReuseIdentifier:@"cell"];
    
    self.tableview.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMoreDataa)];
    [self.view addSubview:self.tableview];
    self.count = 1;
    __weak typeof(self) weakSelf = self;
    _tableview.TapBlok = ^{
        [weakSelf setSearchString:weakSelf.searchString];
    };
}
/**
 *  textFile左边视图  放大镜
 */
-(UIView *)textFileLweftView{
    UIView *bacView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, H(self.TextField), H(self.TextField))];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"btn-search-grey"]];
    imageView.frame = CGRectMake(5,5,W(bacView)-10,W(bacView)-10);
    [bacView addSubview:imageView];
    return bacView;
}

/**
 * textField Deleget  点击键盘右下角搜索  进行的action
 *
 */
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if ([string isEqualToString:@"\n"]) {
        [self.listAry removeAllObjects];
        [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
        [self getDateWhithPage:1 Whithstring:textField.text];
        self.searchString = textField.text;
    }
    return YES;
}
#pragma mark--上拉加载
- (void)getMoreDataa{
    
    if (self.tableview.footer.isRefreshing) {
        
        self.count+=1;
        
        if (self.count>self.infoModel.pageCount.integerValue) {
            
            [self.tableview.footer noticeNoMoreData];
        }else{
            
            [self getDateWhithPage:self.count Whithstring:self.searchString];
        }
    }
}

-(void)getDateWhithPage:(NSInteger)page Whithstring:(NSString *)string{
    
    NSString *url = [NSString stringWithFormat:@"http://school.diaoyur.cn/article/search?page=%ld",(long)page];
    
    [[Manager sharedManager]RequestWhithURL:url AndParameter:@{@"nid":@"all",@"keyword":string} AndCompletionType:^(id responseObject, DIEError *error) {
        
        if (!error) {
            if ([responseObject[@"status"] isEqualToString:@"SUCCESS"]) {
                self.model  = [[ShoolDataModel alloc]initWithDictionary:responseObject[@"data"] error:nil];
                
                self.infoModel = self.model.page_info;
                [self.listAry addObjectsFromArray:self.model.article_list];
                [self.tableview.footer endRefreshing];
                self.tableview.isShowImage = YES;
                [self.tableview reloadData];
            }else{
                [self.tableview.footer endRefreshing];
                self.tableview.isShowImage = YES;
                [self.tableview reloadData];
            }
        }else{
            
            [self.tableview.footer endRefreshing];
            self.tableview.isShowFile = NO;
            [self.tableview reloadData];
        }
        
        
        
    }];
    
}


-(void)setSearchString:(NSString *)searchString{
    _searchString = searchString;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.TextField.text = _searchString;
    });
    
    
    [self getDateWhithPage:1 Whithstring:_searchString];
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.listAry.count;
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return self.cellHight;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.model =_listAry[indexPath.row];
    self.cellHight = cell.cellHight;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (_listAry.count!=0) {
        NEWAticListModel  *Model3 = _listAry[indexPath.row];
        
        if (Model3.skin.integerValue == 1){
            VideoDescrib *video = [VideoDescrib new];
            video.AticleID = Model3.AtcID;
            [self.navigationController  pushViewController:video animated:YES];
        }else{
            NewsCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            NewestAticleCtr *aticle = [NewestAticleCtr new];
            aticle.AticleID = cell.AticleID;
            [self.navigationController pushViewController:aticle animated:YES];
        }
        
    }

}

-(void)didReceiveMemoryWarning{
    
    [super didReceiveMemoryWarning];//即使没有显示在window上，也不会自动的将self.view释放。
    
    if ([self.view window] == nil) {
        
        self.view = nil;// 目的是再次进入时能够重新加载调用viewDidLoad函数。
        
    }
}


@end